const Group = require('../../model/group')
const {isGroup} = require('../dbserver/member')

// 搜索群
exports.searchGroup = async (key,id, res) => {
  try {
    var group = await Group.find({
      name: {
        $regex: key
      }
    })
  } catch (error) {
    return res.send({
      status: 400
    })
  }
  var data =[]
  for(let i=0;i<group.length;i++){
    item=group[i]
    var a =await isGroup(id,item._id)
    if(a){
      data.push({id:item._id,cover:item.cover,name:item.name,isgroup:true})
    }else{
      data.push({id:item._id,cover:item.cover,name:item.name,isgroup:false})
    }
  }
  res.send({
    status: 200, data
  })
}

exports.creategroup = async (obj,res)=>{
  try {
    var a = await Group.create({
     ...obj
    })
  } catch (error) {
    return res.send({
      status:400
    })
  }
  res.send({
    status:200,
    data:a._id
  })
}

exports.exist =  async (name,res)=>{
  var a = await Group.findOne({name})
  if(a){
    res.send({
      status:400
    })
  }else{
    res.send({
      status:200
    })
  }
}

// 搜索群

exports.searchGroupByid = async (id,res)=>{
  try {
    var data = await Group.findOne({_id:id}).select('')
  } catch (error) {
    return res.send({
      status:400
    })
  }
  res.send({
    status:200,data
  })
}

exports.Modify = async(id,obj,res)=>{
  try {
    await Group.updateOne({_id:id},obj)
  } catch (error) {
    return res.send({
      status:400
    })
  }
  res.send({
    status:200
  })
}